﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PartitionsCodegen
{
    class CNCInstruction
    {
        private Hashtable paramsList;

        public CNCInstruction(string aLine)
        {
            paramsList = new Hashtable();
            decimal fvalue;
            int ivalue;
            string key;
            string[] paramsString = aLine.Split(' ');
            foreach (string s in paramsString)
            {
                key = s.Substring(0, 1);
                if (key.Equals("G"))
                {
                    ivalue = Convert.ToInt32(s.Substring(1, s.Length - 1));
                    paramsList.Add(key, ivalue);  
                }
                else {
                    fvalue = Convert.ToDecimal(s.Substring(1, s.Length - 1));
                    paramsList.Add(key, fvalue);  
                }
            }
        }

        public void adjustPosition(int ncopy, decimal size, float z, bool to_the_right)
        {
            if (to_the_right && paramsList.ContainsKey("X"))
            {
                decimal posX = (decimal)paramsList["X"];
                posX = posX + ncopy * size;
                paramsList["X"] = posX;
            }
            else if (!to_the_right && paramsList.ContainsKey("Y"))
            {
                decimal posY = (decimal)paramsList["Y"];
                posY = posY + ncopy * size;
                paramsList["Y"] = posY;
            }

            if (!paramsList.ContainsKey("Z") && (this.getOpCode() == 0 || this.getOpCode() == 1 || this.getOpCode() == 2))
            {
                paramsList.Add("Z", z);
            }
        }

        public void adjustOffset(decimal x, decimal y)
        {
            if (paramsList.ContainsKey("X"))
            {
                decimal posX = (decimal)paramsList["X"];
                posX = posX + x;
                paramsList["X"] = posX;
            }
            if (paramsList.ContainsKey("Y"))
            {
                decimal posY = (decimal)paramsList["Y"];
                posY = posY + y;
                paramsList["Y"] = posY;
            }
        }

        public String getString()
        {
            int opCode=getOpCode();
            string result = "G" + opCode.ToString();
            IDictionaryEnumerator d = paramsList.GetEnumerator();

            while (d.MoveNext())
            {
                if (!d.Key.Equals("G"))
                {
                    result += " " + d.Key;
                    result += d.Value;
                }
            }
            return result;
        }

        public decimal getPosX()
        {
            return (decimal)paramsList["X"];
        }

        public decimal getPosY()
        {
            return (decimal)paramsList["Y"];
        }

        public decimal getPosZ()
        {
            return (decimal)paramsList["Z"];
        }

        public int getOpCode()
        {
            return (int)paramsList["G"];
        }

        public bool hasXValue() 
        {
            return paramsList.ContainsKey("X");
        }

        public bool hasYValue()
        {
            return paramsList.ContainsKey("Y");
        }

        public bool hasZValue()
        {
            return paramsList.ContainsKey("Z");
        }
    }
}

